package com.redteam.xomtro;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.redteam.xomtro.adapters.SubImageDetailsAdapter;
import com.redteam.xomtro.classes.Support;
import com.redteam.xomtro.entities.Room;
import com.redteam.xomtro.fragment.MainFragment;
import com.redteam.xomtro.fragment.SubImageDetailsParallaxFragment;
import com.redteam.xomtro.views.ObservableScrollView;
import com.redteam.xomtro.views.ParallaxTransformer;


public class RoomDetailActivity extends MasterActivity implements View.OnClickListener {
    public static String SELECTED_ROOM = "selected_room";

    ViewPager mPager;
    SubImageDetailsAdapter mAdapter;
        private Room currentRoom;
//    private ImageLoader imageLoader;
//    private DisplayImageOptions options;
//    private DisplayImageOptions optionsAvatar;
//    private TextView roomDiscription;
//    private TextView toggleMore;
//    private GoogleMap googleMap;
    private ObservableScrollView roomScrollView;
    //private ImageView blurImg;
//    private int blurImgWidth;
//    private int blurImgHeight;
//    private StackBlurManager _stackBlurManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_detail);
        if (savedInstanceState != null) {
            currentRoom = (Room) savedInstanceState.getSerializable(SELECTED_ROOM);
        }
        if (currentRoom == null) {
            currentRoom = (Room) getIntent().getSerializableExtra(SELECTED_ROOM);
        }

        final View v = this.findViewById(R.id.rlRoomImageContainer).getRootView();
        getSupportActionBar().setCustomView(R.layout.action_bar_second);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        TextView tvLogo = (TextView) findViewById(R.id.tvLogo);
        tvLogo.setText(getString(R.string.app_name).toLowerCase());
        tvLogo.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/solgas.ttf"));
//        if (savedInstanceState != null) {
//            room = (Room) savedInstanceState.getSerializable("room");
//        }
//        if (currentRoom == null) {
//            currentRoom = (Room) getIntent().getSerializableExtra("selectedRoom");
//        }

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setBackgroundColor(0xFF000000);
        mPager.setPageTransformer(false, new ParallaxTransformer(R.id.parallaxContent));
        mAdapter = new SubImageDetailsAdapter(this, getSupportFragmentManager());
        mAdapter.setPager(mPager);
        for (int i = 0; i < currentRoom.getDemo_subImage().size(); i++) {
            Bundle bSubImage = new Bundle();
            bSubImage.putInt("image",currentRoom.getDemo_subImage().get(i));
            //bSubImage.putInt("index",i);
            //bSubImage.putString("idRoom",currentRoom.getId());
            SubImageDetailsParallaxFragment pfSubImage = new SubImageDetailsParallaxFragment();
            pfSubImage.setArguments(bSubImage);
            mAdapter.add(pfSubImage);
        }
        mPager.setAdapter(mAdapter);
        View loadingImage = null;
        if (findViewById(R.id.pbLoadingImage) != null) {
            loadingImage = findViewById(R.id.pbLoadingImage);
            loadingImage.setVisibility(View.GONE);
        }
        roomScrollView = (ObservableScrollView) findViewById(R.id.svRoomItem);
        //margin description with top image background
        roomScrollView.setScreenHeight(WellcomeActivity.heightScreen / 3 - 150);
        final LinearLayout llClickHideScroll = (LinearLayout) findViewById(R.id.llClickHideScroll);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llClickHideScroll.getLayoutParams();
        params.height = WellcomeActivity.heightScreen / 3;
        llClickHideScroll.setLayoutParams(params);
        llClickHideScroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                roomScrollView.setVisibility(View.GONE);
            }
        });

        roomScrollView.setScrollListener(new ObservableScrollView.ScrollListener() {
            @Override
            public void onScrollUp(float paddingBelowEventTitle, int l, int t, int oldl, int oldt) {
                getSupportActionBar().hide();
                llClickHideScroll.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onScrollDown(float paddingBelowEventTitle, int l, int t, int oldl, int oldt) {
                getSupportActionBar().show();
            }

            @Override
            public void onScrollOnTop(int l, int t, int oldl, int oldt) {
                getSupportActionBar().show();
                llClickHideScroll.setVisibility(View.VISIBLE);
            }

            @Override
            public void onScrollBottom(int l, int t, int oldl, int oldt) {
                getSupportActionBar().show();
            }
        });
        roomScrollView.setCallbacks(new ObservableScrollView.ScrollCallbacks() {
            float alpha = 0f;

            @Override
            public void onScrollChanged(float paddingBelowRoomTitle, int l, int top, int oldl, int oldt) {
                if (Support.isAPIHigherHoneycomb()) {
                    paddingBelowRoomTitle /= 1.2;
                    if ((top - oldt) > 0) /* scroll up */
                        alpha = top / paddingBelowRoomTitle;
                    else if ((top - oldt < 0)) /* scroll down */
                        alpha = top / paddingBelowRoomTitle;
                    if (alpha > 0.9f) alpha = 0.9f;
                    else if (alpha < 0) alpha = 0;
                    if(mPager!=null && mPager.getChildAt(mPager.getCurrentItem())!=null) {
                        if ((mPager.getChildAt(mPager.getCurrentItem())).findViewById(R.id.ivBlurImg) != null) {
                            (mPager.getChildAt(mPager.getCurrentItem())).findViewById(R.id.ivBlurImg).setAlpha(alpha);
                        }
                    }
                }
            }
        });

        TextView roomPrice = (TextView) findViewById(R.id.tvRoomPrice);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTStd-UltLt.ttf");
        roomPrice.setTypeface(tf);
        roomPrice.setTextSize(70);
        roomPrice.setText(currentRoom.getRoom_Price() + "");

        TextView subRoomPrice = (TextView) findViewById(R.id.tvSubRoomPrice);
        subRoomPrice.setTypeface(tf);
        subRoomPrice.setTextSize(30);
        subRoomPrice.setText("Tr");


        TextView roomName = (TextView) findViewById(R.id.tvRoomName);
        roomName.setTypeface(MainFragment.fontLight);
        roomName.setText(currentRoom.getRoom_Name());

        TextView roomAddress = (TextView) findViewById(R.id.tvRoomAddress);
        roomAddress.setTypeface(MainFragment.fontLight);
        roomAddress.setText(currentRoom.getRoom_Address());

        GridView gvTienIch = (GridView) findViewById(R.id.gvTienIch);
        String[] tes = {"4 phòng", "WC Riêng", "Sạch sẽ", "Thoáng mát", "Gần chợ", "Gần trường"};
        ArrayAdapter<String> aa = new ArrayAdapter<String>(
                this,
                R.layout.custom_text_view_gv,
                tes);
        gvTienIch.setAdapter(aa);

        ImageView staticMap = (ImageView) findViewById(R.id.staticRoomMap);
        String staticMapUrl = "http://maps.googleapis.com/maps/api/staticmap?center=" + currentRoom.getRoom_Latitude()
                + "," + currentRoom.getRoom_Longitude() + "&zoom=15&size=" + Support.getScreenWidth(this) + "x"
                + (int) getResources().getDimension(R.dimen.room_map_height) + "&sensor=false" +
                "&markers=color:red%7C" + currentRoom.getRoom_Latitude() + "," + currentRoom.getRoom_Longitude();
        imageLoader.displayImage(staticMapUrl,staticMap);
        //staticMap.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.tvEventAttendeesLeft:
//                attendeesDialog.show();
//                break;
//            case R.id.tvToggleMore:
//                if (eventDiscription.getTag().toString().equals("collapsed")) {
//                    eventDiscription.setMaxLines(Integer.MAX_VALUE);
//                    eventDiscription.setTag("expanded");
//                    toggleMore.setText(context.getString(R.string.txt_event_read_less));
//                } else {
//                    eventDiscription.setTag("collapsed");
//                    eventDiscription.setMaxLines(5);
//                    toggleMore.setText(context.getString(R.string.txt_event_read_more));
//                }
//                break;
//            case R.id.staticEventMap:
//                AlertDialog.Builder builder = new AlertDialog.Builder(context);
//                ViewGroup viewContent = (ViewGroup) View.inflate(context, R.layout.item_room_details_map_expanded, null);
//                builder.setView(viewContent);
//                builder.setCancelable(true);
//                final AlertDialog alertDialog = builder.create();
//                View btnClose = viewContent.getChildAt(1);
//                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.title_event_map));
//                if (mapFragment != null) {
//                    googleMap = mapFragment.getMap();
//                }
//
//                if (btnClose != null)
//                    btnClose.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            alertDialog.dismiss();
//                        }
//                    });
//                alertDialog.show();
//                break;
//            case R.id.btnAttend:
//                Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
//                break;
//        }
    }

//    class BlurImageAsyncTask extends AsyncTask<Void, Bitmap, Bitmap> {
//        @Override
//        protected Bitmap doInBackground(Void... iv) {
//            try {
//                return _stackBlurManager.process(100);
//            } catch (Exception ex) {
//                return null;
//            }
//        }
//
//        @Override
//        protected void onPostExecute(Bitmap bitmap) {
//            blurImg.setImageBitmap(Bitmap.createScaledBitmap(bitmap, blurImgWidth, blurImgHeight, false));
//        }
//    }
}
